package org.dbstar.scheduler.beans;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import net.sf.json.JSONObject;

import org.apache.commons.lang.time.DateFormatUtils;
import org.dbstar.scheduler.control.JobController;
import org.dbstar.scheduler.identifier.Identifier;


/**
 * 使用文件来存储执行结果的job结果类
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-3-21
 */
public class FileCmdJobResult extends CmdJobResult {
	private static final long serialVersionUID = 3729009850016271953L;

	private File resultRoot;
	private String out;
	private String err;
	private File outFile;
	private File errFile;

	public final void setResultRoot(File resultRoot) {
		this.resultRoot = resultRoot;
		initFile();
	}

	public final void setOut(String outFile) {
		this.out = outFile;
		initFile();
	}

	public final void setErr(String errFile) {
		this.err = errFile;
		initFile();
	}

	public final String getOut() {
		return out;
	}

	public final String getErr() {
		return err;
	}

	@SuppressWarnings("resource")
	@Override
	public final InputStream getStandardStream() throws IOException {
		return outFile.isFile() ? new FileInputStream(outFile) : null;
	}

	@SuppressWarnings("resource")
	@Override
	public final InputStream getErrorStream() throws IOException {
		return errFile.isFile() ? new FileInputStream(errFile) : null;
	}

	@Override
	public final OutputStream getStdOutStream() throws IOException {
		return new FileOutputStream(outFile, true);
	}

	@Override
	public final OutputStream getStdErrStream() throws IOException {
		return new FileOutputStream(errFile, true);
	}

	private void initFile() {
		outFile = resultRoot != null && out != null ? new File(resultRoot, out) : null;
		errFile = resultRoot != null && err != null ? new File(resultRoot, err) : null;
	}

	@Override
	public final void init(Job job, JobController jobController) {
		if (out == null) {
			if (job.getIdentifier() == null) {
				throw new IllegalArgumentException("Job's identifier not set.");
			}
			String jobResultRoot = getJobResultRoot(job.getSubmitTime(), job.getIdentifier());
			setOut(jobResultRoot + File.separatorChar + "stdout.txt");
			setErr(jobResultRoot + File.separatorChar + "stderr.txt");
		}
		mkdir(outFile);
		mkdir(errFile);
	}

	private static String getJobResultRoot(long submitTime, Identifier jobIdentifier) {
		return getJobResultRoot(getDayResultRoot(submitTime), jobIdentifier);
	}

	private static String getJobResultRoot(String dayResultRoot, Identifier jobIdentifier) {
		return dayResultRoot + File.separatorChar + jobIdentifier.getStringIdentifier();
	}

	private static String getDayResultRoot(long submitTime) {
		return DateFormatUtils.format(submitTime, "yyyyMMdd");
	}

	private static void mkdir(File file) {
		File parent = file.getParentFile();
		if (!parent.isDirectory()) {
			parent.mkdirs();
		}
	}

	@Override
	protected void write(JSONObject object) {
		super.write(object);
		if (out != null) {
			object.element("out", out);
		}
		if (err != null) {
			object.element("err", err);
		}
	}

	@Override
	protected void read(JSONObject object) {
		super.read(object);
		setOut(object.optString("out", null));
		setErr(object.optString("err", null));
	}
}
